#include "list.h"


/////////////////////////////////////////
// Constructor
/////////////////////////////////////////

List::List(int listSize) : m_MaxSize(listSize), m_list(listSize)
{
   m_numInList = 0;
   whichToOverWrite = 0;
}


// Adds a hypsphere object to the tabu list (no longer used?)
void List::add(const Hypersphere &h)
{
  m_list[whichToOverWrite] = h;

  if (whichToOverWrite == m_MaxSize - 1)
  {
    whichToOverWrite = 0;
  }
  else
  {
    whichToOverWrite++;
  }

  if (m_numInList < m_MaxSize)
  {
    m_numInList++;
  }

}

// Adds a 'Hypersphere' object in the form of a solution and a radius
void List::add(int numDimension, double *solution, double radius)
{

  m_list[whichToOverWrite].setRegion(numDimension, solution, radius);

  if (whichToOverWrite == m_MaxSize - 1)
  {
    whichToOverWrite = 0;
  }
  else
  {
    whichToOverWrite++;
  }

  if (m_numInList < m_MaxSize)
  {
    m_numInList++;
  }

}

void List::replace(int numDimension, double *solution, double radius, double funcval)
{
  double worst = 0.0;
  int worstsol = 0;
  double wValue = 0;

  worst = m_list[0].m_funcval;

  for (unsigned int i = 1; i < m_list.size(); i++)
  {
    wValue = m_list[i].m_funcval;
    if(worst < wValue)
    {
      worst = wValue;
      worstsol = i;
    }
  }

  m_list[worstsol].setRegion(numDimension, solution, radius, funcval);

}

// Checks to see if the solution is inside any of the tabu balls
// inside the tabu list
bool List::isInList(double *solution)
{
  for (int i = 0; i < m_numInList; i++)
  {
    if (true == m_list[i].isInRegion(solution))
    {
      return true;
    }
  }

  return false;
}

// vector version
bool List::isInList(const vector<double> &solution)
{
  for (int i = 0; i < m_numInList; i++)
  {
    if (true == m_list[i].isInRegion(solution))
    {
      return true;
    }
  }

  return false;
}